package com.ssh;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class RomanToInt_13 {
	public int romanToInt(String s) {
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		map.put('I', 1);
		map.put('V', 5);
		map.put('X', 10);
		map.put('L', 50);
		map.put('C', 100);
		map.put('D', 500);
		map.put('M', 1000);
		int sum = 0, n = s.length() - 1;
		char t = '$';
		for (int i = n; i >= 0; i--) {
			char c = s.charAt(i);
			int val = map.get(c);
			if (val < sum && t != c) {
				sum -= val;
			}else {
				sum += val;
			}
			t = c;
		}
		return sum;
    }
	
	@Test
	public void test () {
		System.out.println(romanToInt("V"));
	}
}
